<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>新增数据库</title>
    <link rel="stylesheet" href="/lay/layui/css/layui.css">
    <script src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/lay/layer/layer.js"></script>
    <script src="/lay/layui/layui.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>

</head>
<body class="layui-layout-body">
<div class="create-db-content">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="db_name" required lay-verify="required" placeholder="请输入数据库名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字符集</label>
            <div class="layui-input-inline">
                <select name="charset" lay-verify="required" id="charset_list" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
            </div>
        </div>
        <div class="layui-form-item">
                <p id="error_tip"></p>
        </div>
    </form>
</div>
</body>
<script>
    layui.use('form', function(){
        var form = layui.form;
        form.render();
    });
    $(function () {
        var postData = {};
        var res = sendAjaxRequest('GET', '/database/getAllCharset', postData, false);
        console.log(res)
        if (res.status == 0) {
            for (var i=0; i < res.data.length; i++) {
                $('#charset_list').append('<option value="'+res.data[i].Charset+'&'+res.data[i]['Default collation']+'">'+res.data[i]['Default collation']+'</option>')
            }
        }
    })


    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            data.field._token = $('meta[name="csrf-token"]').attr('content');
            // layer.msg(JSON.stringify(data.field));
            var res = sendAjaxRequest('POST', '/database/createDatabase', data.field, false);
            if (res.status == 0) {
                layer.msg('创建成功');
                parentReloadPage();
            } else {
                $('#error_tip').html('<font color="red">'+res.msg+'</font>');
                // layer.msg(res.msg);
            }
            return false;
        });
    });
</script>
</html>